#!coding=utf-8

import threading
import time

# 创建一个信号量
semaphore = threading.Semaphore(2)

def worker():
    # 获取信号量
    semaphore.acquire()
    try:
        # 访问共享数据
        print(f"Thread {threading.current_thread().ident}is working...")
        time.sleep(3)
    finally:
        # 释放信号量
        semaphore.release()

# 创建三个线程
thread1 = threading.Thread(target=worker)
thread2 = threading.Thread(target=worker)
thread3 = threading.Thread(target=worker)

# 启动线程
thread1.start()
thread2.start()
thread3.start()

# 等待所有线程结束
thread1.join()
thread2.join()
thread3.join()